//
//  TasbihListController.m
//  Beads
//
//  Created by CPX on 2018/11/1.
//  Copyright © 2018 cpx. All rights reserved.
//

#import "TasbihListController.h"
#import "TasbihTableViewCell.h"
#import "BeadsApi.h"
@interface TasbihListController ()
<
UITableViewDelegate,
UITableViewDataSource
>
@property (nonatomic,strong)UITableView * tableView;

@end

@implementation TasbihListController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}
- (void)initViews{
    [self.view addSubview:self.tableView];
    
}
- (void)initNavigation{
    [self initDefaultNavigation];
    [self.myNavigationBar.backButton setTitle:@"Tasbih" forState:UIControlStateNormal];
  
}
- (void)loadData{
    [BeadsApi getDailyProverArrayWithModelClass:[TasbihModel class]
                                 NormalResponse:^(NSInteger status, id  _Nonnull data, NSMutableArray * _Nonnull array) {
                                     [self.dataArray removeAllObjects];
                                     [self.dataArray addObjectsFromArray:array];
                                     [self.tableView reloadData];
    } exceptionResponse:^(NSError * _Nonnull error) {
        
    }];
}

+ (void)getDefaultProverbtext:(void (^)(NSString * _Nonnull))completion{
    [BeadsApi getDailyProverArrayWithModelClass:[TasbihModel class]
                                 NormalResponse:^(NSInteger status, id  _Nonnull data, NSMutableArray * _Nonnull array) {
                                     if (array.count){
                                         if (completion){
                                             TasbihModel * first = array.firstObject;
                                             completion(first.proverb_text);
                                         }
                                     }
                                     
                                 } exceptionResponse:^(NSError * _Nonnull error) {
                                     
                                 }];
}


#pragma mark -----uitableview datasource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TasbihTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:@"TasbihTableViewCell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.dataArray[indexPath.section];
    [cell.rankLabel setTitle:[NSString stringWithFormat:@"%ld",indexPath.section] forState:UIControlStateNormal];
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.0001;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}


-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 15;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TasbihModel * model = self.dataArray[indexPath.section];
    if (self.proverbTextSelectBlock){
        self.proverbTextSelectBlock(model.proverb_text);
    }
}

#pragma mark ---getter
-(UITableView * )tableView{
    if (!_tableView){
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(12, NavHeight, self.view.bounds.size.width - 12* 2, self.view.bounds.size.height -NavHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = UIColor.clearColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView registerNib:[UINib nibWithNibName:@"TasbihTableViewCell" bundle:nil] forCellReuseIdentifier:@"TasbihTableViewCell"];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
- (NSMutableArray *)dataArray{
    if (!_dataArray){
        _dataArray = [NSMutableArray new];
    }
    return _dataArray;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
